package whu.edu.assignment4.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import whu.edu.assignment4.dataobject.User;
import whu.edu.assignment4.mapper.UserMapper;
import whu.edu.assignment4.service.UserService;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    //根据id查找用户
    public User findById(int uid) {
        return userMapper.selectUserByUid(uid);
    }

    //用户注册
    public int registration(User user) {
        User same = userMapper.selectUserByUsername(user.getUsername());
        if(same != null) {
            return -1;
        }
        else {
            int result = userMapper.insertUser(user);
            return result;
        }
    }

    public int login(User user) {
        User exist = userMapper.selectUserByUsername(user.getUsername());
        if(exist == null) {
            return -1;
        }
        if(exist.getPassword().equals(user.getPassword())) {
            return 1;
        }
        else {
            return 0;
        }
    }

    public List<User> findAll() {
        return userMapper.selectAll();
    }
}
